<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class Member extends BaseModel
{

    use SoftDeletes;

    public $table = 'member';
    public $timestamps = true;
    static $unguarded = true;

    const CREATED_AT = 'reg_time';
    const UPDATED_AT = 'update_time';

    protected $hidden = [
        "reg_ip",
        "reg_ip_state",
        "reg_ip_address",
        "original",
        "reg_ip_operator"
    ];

    /**
     * 获取当前时间
     *
     * @return int
     */
    public function freshTimestamp()
    {
        return time();
    }

    public function combo()
    {
        return $this->hasOne(MemberComboMapping::class, 'uid', 'id')->select("end_time");
    }

    /**
     * 避免转换时间戳为时间字符串
     *
     * @param DateTime|int $value
     * @return DateTime|int
     */
    public function fromDateTime($value)
    {
        return $value;
    }

    public static function getMobilerea($country, $phone)
    {
        if (!isset($phone) || empty($phone)) {
            return '';
        }

        //国外号码
        if (!empty($country) && $country != 1) {
            return Country::where(['id' => $country])->value('cn_name');
        }

        $phone = substr($phone, 0, 7);
        $phones = Phones::where('number', "=", $phone)->first();
        if ($phones) {
            $regions = Regions::where("id", "=", $phones['region_id'])->first();
            $arr = array('北京', '上海', '天津', '重庆');

            if (in_array($regions['province'], $arr)) {
                return $regions['province'];
            } else {
                return $regions['province'] . $regions['city'];
            }

        } else {
            //不存在的记录下下来，维护进去
            file_put_contents('/tmp/phone.log', date('Y-m-d H:i:s') . '  ' . $phone . "\n\n", 8);
            return '暂无数据';
        }
    }

    static function typeList($v = "")
    {
        $arr = [
            10001 => 2, //职工
            10002 => 1  //病患
        ];

        return strlen($v) ? $arr[$v] : $arr;
    }

}
